package com.example.admin.sjb;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.Random;

public class ComputerActivity extends AppCompatActivity {

    private TextView tv1;
    private TextView tv2;
    private ImageView iv;
    private boolean isRun=true;//控制无线循环的变量

    private int random;//电脑出拳的随机数
    private int play;//用户出拳
    private int number=500;//
    private Button bt1;
    private Button bt2;
    private Button bt3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_computer);
        //1.findViewById
        tv1=(TextView)findViewById(R.id.textView);
        tv2=(TextView)findViewById(R.id.textView2);
        iv=(ImageView)findViewById(R.id.imageView);
        bt1=(Button)findViewById(R.id.button);
        bt2=(Button)findViewById(R.id.button2);
        bt3=(Button)findViewById(R.id.button3);


        tv2.setText("分数:"+number);
        start();//动态改变图片的方法
    }
    /**
     * 开启线程动态更新图片
     *
     * */
    private void start() {
        //1.开启线程
        new Thread(){
            @Override
            public void run() {
                while(isRun){
                    random=new Random().nextInt(3);//0-2
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            //更新图片
                            if(random==0){
                                iv.setImageResource(R.drawable.shi);
                            }
                            if(random==1){
                                iv.setImageResource(R.drawable.jian);
                            }
                            if(random==2){
                                iv.setImageResource(R.drawable.bu);
                            }
                        }
                    });//主线程更新UI操作
                    try {
                        Thread.sleep(20);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }

    //点击石头按钮
    public void Shi(View view){
        play=0;//石头
        isRun=false;
        //判断胜负
        Shengfu();
    }


    //点击剪刀按钮
    public void Jian(View view){
        play=1;//石头
        isRun=false;
        //判断胜负
        Shengfu();
    }
    //点击布按钮
    public void Bu(View view){
        play=2;//石头
        isRun=false;
        //判断胜负
        Shengfu();
    }
    public void Shengfu(){
        if(play==random){
            tv1.setText("平手");
        }
        else if((play==0&&random==1)||(play==1&&random==2)||(play==2&&random==0)){
            tv1.setText("玩家赢");
            number+=100;
        }else{
            tv1.setText("玩家输");
            number-=100;
        }
        tv2.setText("分数:"+number);
        bt1.setEnabled(false);//按钮不可用
        bt2.setEnabled(false);//按钮不可用
        bt3.setEnabled(false);//按钮不可用


    }



    //点击重新开始按钮
    public void ReStart(View view){
        play=0;
        random=0;
        tv1.setTag("胜负");
        isRun=true;
        bt1.setEnabled(true);//按钮不可用
        bt2.setEnabled(true);//按钮不可用
        bt3.setEnabled(true);//按钮不可用
        start();
    }


}

